package com.hqyj.hrms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class MyEmail {

    //注入邮件发送类
    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 发送邮件
     *
     * @param email   收件人的邮箱
     * @param content 邮件正文
     * @return
     */
    public HashMap<String, Object> sendEmail(String email, String content, String title) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            //创建一个邮件
            SimpleMailMessage message = new SimpleMailMessage();
            //写入收件人邮箱
            message.setTo("1428282641@qq.com");
            //写入发件人邮箱
            message.setFrom("767920412@qq.com");
            //写入邮件标题
            message.setSubject(title);
            //写入邮件正文
            message.setText(content);
            //发送邮件
            javaMailSender.send(message);
            map.put("info", "发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("info", "发送失败");
        }
        return map;
    }
}
